package co.yixiang.modules.shop.service.impl;

import co.yixiang.modules.shop.domain.Sales;
import co.yixiang.modules.shop.repository.SalesRepository;
import co.yixiang.modules.shop.service.SalesService;
import co.yixiang.modules.shop.service.dto.SalesDTO;
import co.yixiang.modules.shop.service.mapper.SalesMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

/**
 * @author frq
 * @date 2020-06-11
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class SalesServiceImpl implements SalesService {

    @Autowired
    private SalesRepository salesRepository;
    @Autowired
    private SalesMapper salesMapper;


    /**
     * 通过账户查询业务员
     *
     * @param userName
     * @return
     */
    @Override
    public Sales findUserNameUser(String userName) {
        Sales yxSales = new Sales();
        yxSales.setStatus(true);
        yxSales.setUsername(userName);
        Example<Sales> example = Example.of(yxSales);
        Optional<Sales> optional = salesRepository.findOne(example);
        if (optional.isPresent()) {
            return optional.get();
        } else {
            return null;
        }
    }

    @Override
    public SalesDTO findById(Integer id) {
        Optional<Sales> optional=salesRepository.findById(id);
        if(optional.isPresent()){
            return salesMapper.toDto(optional.get());
        }
        return null;
    }

}